package ac.github.os.internal.core.action.location.particle

import ac.github.os.api.OriginSkillAPI
import ac.github.os.internal.core.action.ILocationAction
import ac.github.os.internal.core.action.location.particle.matrix.MatrixAdapter
import ac.github.os.internal.core.config.StringConfig
import ac.github.os.internal.core.config.get
import org.bukkit.Location
import taboolib.common5.Coerce
import taboolib.module.effect.ParticleObj
import taboolib.platform.util.toProxyLocation

abstract class IParticleAction : ILocationAction() {

    abstract val particleName: String

    override val namespace: String
        get() = "loc:particle:$particleName"

    fun Location.adapt(): taboolib.common.util.Location {
        return toProxyLocation()
    }

    fun StringConfig.angle(double: Double = 30.0): Double {
        return get("angle", double)!!
    }

    fun ParticleObj.inject(config: StringConfig): ParticleObj {
        config.get<List<String>>("matrix")?.map {
            val dataPair = OriginSkillAPI.check(it)
            MatrixAdapter.impls.firstOrNull { it.namespace == dataPair.key }?.toMatrix(dataPair.value.apply { initial(config.session) })
        }?.forEach { it?.apply {
            this@inject.addMatrix(this)
        } }
        return this
    }

    fun StringConfig.step(double: Double = 1.0): Double {
        return get("step", double)!!
    }

    fun StringConfig.radius(double: Double = 1.0): Double {
        return get("radius", double)!!
    }

    fun StringConfig.spawner(): OriginParticleSpawner {
        return OriginParticleSpawner(session, this)
    }

    fun StringConfig.offset(location: Location): Location {
        val offset = get("offset", listOf("0.0", "0.0", "0.0"))!!.map { Coerce.toDouble(it) }
        return location.clone().add(offset[0], offset[1], offset[2])
    }

}